<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        table {
            border: 1px solid #e0e0e0;
            border-collapse: collapse;
        }

        td {
            border: 1px solid #e0e0e0;
        }
    </style>
</head>

<body>
    <header class="aui-bar aui-bar-nav main-header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            容器解绑
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header"></div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;padding-bottom: 50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">容器条码</div>
                        <input type="text" class="aui-input" placeholder="请扫描容器条码" style="width: 80%;" id="trayyinfoid"
                            onclick="getIdSelector();">

                    </div>
                </li>

            </ul>
        </div>
        <div class="white_background">
            <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="wmTrayTbindByNum()"
                id="submitButton">录数解绑</div>
            <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="wmTrayTbindBySum()"
                id="submitButtonAll">批量解绑
            </div>
            <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="wmTrayTbind()"
                id="submitButtonTotal">一键解绑</div>
        </div>
        <div class="white_background">
            <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom: 2%;">
                <table class="table" style='width:100%;' id="table">
                    <thead>
                        <tr style="text-align: center;color: #757575;font-size: 8px;font-weight: bold;">
                            <td algin='center'></td>
                            <td algin='center'>物料编码</td>
                            <td algin='center'>物料名称</td>
                            <td algin='center'>批次号</td>
                            <td algin='center'>数量</td>
                        </tr>
                    </thead>
                    <tbody id="tbodyHtml">
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    //初始化
    apiready = function () {
        $("#trayyinfoid").focus();
        idSelector = "trayyinfoid";

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：容器条码
            if (idSelector == 'trayyinfoid') {

                $("#trayyinfoid").val(ret);
                getTrayInfo();
                //2:库位
            }

        });
    }
    //容器监听回车事件
    $("#trayyinfoid").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                console.log(tm);
                $("#trayyinfoid").val(tm);
                $("#tm").focus();
                getTrayInfo();
            }

        }
    });

    function getTrayInfo() {
        var sm = $("#trayyinfoid").val();
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/doSearchTrayac";
        var data = {
            values: {
                sm: sm,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: black;font-size: 10px; '>";
                        html += "<td ><input name='sub' type='checkbox' /></td>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td name='ext05'>" + list[i].ext05 + "</td>";
                        html += "<td name='qty'>" + list[i].qty + "</td>";
                        html += "<td name='sysid' style='display:none'>" + list[i].sysid + "</td>";
                        html += "</tr>";
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //失败
                } else if (myobj.code == "1") {
                    $("#trayyinfoid").val("");
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#trayyinfoid").val("");
                $("#trayyinfoid").select();
                idSelector = "trayyinfoid";
                api.hideProgress();
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }


    //录数解绑
    function wmTrayTbindByNum() {
        if (!clickThrottle()) {
            return;
        }
        var trayyinfoid = $("#trayyinfoid").val();
        const submitButton = document.getElementById('submitButton');

        submitButton.style.pointerEvents = "none"
        //容器
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描容器！',
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });


        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }
        var qty = 0
        var list = [];
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            var sysid = row.find("[name='sysid']").html();
            qty = row.find("[name='qty']").text();
            list.push({
                "sysid": sysid
            });
        });
        var dialogBox = api.require('dialogBox');
        dialogBox.input({
            keyboardType: 'default',
            texts: {
                title: '数量',
                placeholder: '数量',
                leftBtnTitle: '取消',
                rightBtnTitle: '确定'
            },
            styles: {
                bg: '#fff',
                corner: 2,
                w: 300,
                h: 200,
                title: {
                    h: 40,
                    alignment: 'center',
                    size: 14,
                    color: '#000',
                    marginT: 30,
                },
                input: {
                    h: 60,
                    y: 30,
                    marginT: 15,
                    marginLeft: 10,
                    marginRight: 10,
                    textSize: 14,
                    textColor: '#000',
                    corner: 5, //（可选项）数字类型；对输入框圆角大小；默认：0

                },
                dividingLine: {
                    width: 0.5,
                    color: '#696969'
                },
                left: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40, //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60, //(可选项) 数字类型；左边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10, //(可选项) 数字类型；左边按离底部的边距；默认：0
                    corner: 5, //(可选项) 数字类型；左边按钮的圆角；默认：0
                    widhtBorder: 1, //(可选项) 数字类型；左边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23' //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                },
                right: {
                    bg: 'rgba(0,0,0,0)',
                    color: '#007FFF',
                    size: 12,
                    h: 40, //(可选项) 数字类型；右边按钮的高度；默认：40
                    w: 60, //(可选项) 数字类型；右边按钮的宽度；默认：提示框的宽度的一半
                    marginB: 10, //(可选项) 数字类型；右边按离底部的边距；默认：0
                    corner: 5, //(可选项) 数字类型；右边按钮的圆角；默认：0
                    widhtBorder: 1, //(可选项) 数字类型；右边按钮的边宽；默认：0
                    normalBorderColor: '#778899', //(可选项) 数字类型；右边按钮的边宽颜色；默认：'rgba(0,0,0,0)'
                    activeBorderColor: '#0000CD', //(可选项) 数字类型；右边按钮的高亮状态下边宽颜色；默认：'rgba(0,0,0,0)'
                    activeColor: '#006400', //(可选项) 数字类型；右边按钮的高亮状态下标题颜色；默认：'rgba(0,0,0,0)'
                    activeBg: '#6B8E23' //(可选项) 数字类型；右边按钮的高亮状态下背景颜色；默认：'rgba(0,0,0,0)'
                }
            }
        }, function (ret) {
            if (ret.eventType == 'left') {
                closeDialog();
                submitButton.style.pointerEvents = "auto"
            } else {
                var newQty = ret.text;
                if (newQty == null || newQty == "" || newQty == undefined || newQty <= 0) {
                    api.toast({
                        msg: '数量必须大于0!',
                        duration: 3000
                    });
                    closeDialog();
                    submitButton.style.pointerEvents = "auto"
                    return;
                }
                if (Number(newQty) > Number(qty)) {
                    api.toast({
                        msg: '解绑数量大于明细数量，无法操作！',
                        duration: 3000
                    });
                    closeDialog();
                    submitButton.style.pointerEvents = "auto"
                    return;
                }
                closeDialog();
                api.showProgress({
                    title: '解绑中...',
                });
                var sm = $("#trayyinfoid").val();
                var un = localStorage.getItem('un');
                var serverUrl = localStorage.getItem('serverUrl');
                var url = serverUrl + "/app/wmTrayTbindByNum";
                var data = {
                    values: {
                        list: JSON.stringify(list),
                        qty: newQty,
                        trayyinfoid: trayyinfoid,
                        un: un
                    }
                };
                api.ajax({
                    url: url,
                    method: 'post',
                    dataType: 'json',
                    timeout: 300,
                    returnAll: false,
                    data: data
                }, function (ret, err) {
                    if (ret) {
                        var data = JSON.stringify(ret);
                        var myobj = eval("(" + data + ")");
                        if (myobj.code == '0') {
                            api.hideProgress();
                            submitButton.style.pointerEvents = "auto"
                            api.toast({
                                msg: "解绑成功",
                                duration: 3000
                            });

                            getTrayInfo()

                            //光标回到箱码
                            $("#trayyinfoid").focus();
                            $("#trayyinfoid").select();
                            idSelector = "trayyinfoid";
                        } else {

                            //光标回到箱码
                            $("#trayyinfoid").focus();
                            $("#trayyinfoid").select();
                            idSelector = "trayyinfoid";
                            api.hideProgress();
                            submitButton.style.pointerEvents = "auto"
                            api.toast({
                                msg: myobj.message,
                                duration: 3000
                            });
                        }
                    } else {

                        //光标回到箱码
                        $("#trayyinfoid").focus();
                        $("#trayyinfoid").select();
                        idSelector = "trayyinfoid";
                        api.hideProgress();
                        submitButton.style.pointerEvents = "auto"
                        // alert(JSON.stringify(err));
                        alert("连接超时，请耐心等待");
                    }
                });
            }
        });

    }


    //录包装数打印
    function wmTrayTbindBySum() {
        if (!clickThrottle()) {
            return;
        }
        var trayyinfoid = $("#trayyinfoid").val();
        const submitButtonAll = document.getElementById('submitButtonAll');

        submitButtonAll.style.pointerEvents = "none"
        //容器
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描容器！',
                duration: 3000
            });
            submitButtonAll.style.pointerEvents = "auto"
            return;
        }

        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function () {
            count++;
        });


        //选中行不为1
        if (count == 0) {
            api.toast({
                msg: '请选择至少一条数据！',
                duration: 3000
            });
            submitButtonAll.style.pointerEvents = "auto"
            return;
        }
        var qty = 0
        var list = [];
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            var sysid = row.find("[name='sysid']").html();
            qty = row.find("[name='qty']").html();
            list.push({
                "sysid": sysid
            });
        });
        api.showProgress({
            title: '解绑中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmTrayTbindBySum";
        var data = {
            values: {
                list: JSON.stringify(list),
                trayyinfoid: trayyinfoid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                if (myobj.code == "0") {
                    api.hideProgress();
                    submitButtonAll.style.pointerEvents = "auto"
                    api.toast({
                        msg: "解绑成功",
                        duration: 3000
                    });

                    getTrayInfo()

                    //光标回到箱码
                    $("#trayyinfoid").focus();
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";
                } else if (myobj.code == "1") {
                    $("#trayyinfoid").val("");
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.hideProgress();
                    submitButtonAll.style.pointerEvents = "auto"
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                $("#trayyinfoid").val("");
                $("#trayyinfoid").select();
                idSelector = "trayyinfoid";
                api.hideProgress();
                submitButtonAll.style.pointerEvents = "auto"
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });
    }


    //一键解绑
    function wmTrayTbind() {
        if (!clickThrottle()) {
            return;
        }
        var trayyinfoid = $("#trayyinfoid").val();
        const submitButtonTotal = document.getElementById('submitButtonTotal');

        submitButtonTotal.style.pointerEvents = "none"
        //容器
        if (trayyinfoid == "" || trayyinfoid == null || trayyinfoid == undefined) {
            api.toast({
                msg: '请扫描容器！',
                duration: 3000
            });
            submitButtonTotal.style.pointerEvents = "auto"
            return;
        }
        api.showProgress({
            title: '解绑中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wmTrayTbind";
        var data = {
            values: {
                trayyinfoid: trayyinfoid,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    api.hideProgress();
                    submitButtonTotal.style.pointerEvents = "auto"
                    api.toast({
                        msg: "解绑成功",
                        duration: 3000
                    });

                    getTrayInfo()

                    //光标回到箱码
                    $("#trayyinfoid").focus();
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";
                } else {

                    //光标回到箱码
                    $("#trayyinfoid").focus();
                    $("#trayyinfoid").select();
                    idSelector = "trayyinfoid";
                    api.hideProgress();
                    submitButtonTotal.style.pointerEvents = "auto"
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {

                //光标回到箱码
                $("#trayyinfoid").focus();
                $("#trayyinfoid").select();
                idSelector = "trayyinfoid";
                api.hideProgress();
                submitButtonTotal.style.pointerEvents = "auto"
                // alert(JSON.stringify(err));
                alert("连接超时，请耐心等待");
            }
        });

    }



    function closeDialog() {
        var dialogBox = api.require('dialogBox');
        dialogBox.close({
            dialogName: 'input'
        });
    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>